package com.vsoft.sms_cm;

import java.io.DataOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.log4j.Logger;

import com.vsoft.framework.core.BaseException;
import com.vsoft.framework.core.BusiObj;
import com.vsoft.framework.core.Configuration;
import com.vsoft.framework.core.db.DBGadgets;
import com.vsoft.framework.core.db.DBTrans;
import com.vsoft.framework.core.esb.ServiceBus;
import com.vsoft.framework.core.util.BOHelper;
import com.vsoft.framework.core.util.LogCollect;
import com.vsoft.framework.core.util.XMLUtil;
import com.vsoft.framework.server.servlet.SessionData;
import com.vsoft.framework.server.servlet.HttpUtil;
import com.vsoft.anti_fra.*;

public class TestCmPublic {

	private static Logger vLogger = Logger.getLogger(TestQuery.class);
	private static String vName = TestCmPublic.class.getName();
	private SessionData vSD = null;
	private String vDebugHome = "";
	private BusiObj				vInBO		=  new BusiObj();
	
	
	public static void main(String[] args)  {
	
		try{
/*			response.reset();
			   response.setCharacterEncoding("GBK");
			   response.setContentType("application nd.ms-excel;charset=GBK");
			   String fileName = new String("通讯录.xls".getBytes("GBK"), "iso-8859-1");      //设置导出文件中文名编码
			   response.setHeader("content-disposition", "attachment; filename=" + fileName);
			   DataOutputStream stream = new DataOutputStream(response.getOutputStream());
			   workbook.write(stream);
			   stream.flush();
			   stream.close();*/
			vLogger.info("---------------InitSystem-------------");
			Configuration.InitSystem();
			vLogger.info("---------------generate test data-------------");
/*			String order_num="11111";
			Connection conn1=DBTrans.getConnection();
			//Connection conn1=DBTrans.getConnection("sms_in");
			Customer.getSubscriberAndSMSTemple(order_num, conn1);*/
			TestCmPublic t = new TestCmPublic();
			
			//TestQuery t = new TestQuery();
			Connection conn = DBTrans.getConnection();
			CmPublic __CmPublic=new CmPublic(conn);
			String seq=__CmPublic.genSequenceForOrder("order_no", conn);
			vLogger.info("---------------seq-------------"+seq);
			
			ArrayList<String> tablesName=new ArrayList<String>();
			tablesName.add("service_tables");
			tablesName.add("service_table_relations");
			HashMap<String,String>hRelationColumns=new HashMap<String,String>();
			ArrayList<HashMap<String,String>>aRelationColumn =new ArrayList<HashMap<String,String>>();
			hRelationColumns.put("service_table_id", "service_table_id");
			hRelationColumns.put("ta","service_tables");
			hRelationColumns.put("tb","service_table_relations");
			aRelationColumn.add(hRelationColumns);
			HashMap<String,String>hRelationColumns1=new HashMap<String,String>();
			hRelationColumns1.put("service_table_id", "1");
			hRelationColumns1.put("ta","service_tables");
			aRelationColumn.add(hRelationColumns1);
			BusiObj		inBO	=	new BusiObj();
			inBO.setName("getProductRelation");
			inBO.setString("service_table_id", "3");
			inBO.setConnection(conn);
			BusiObj retBo=__CmPublic.getServiceTableRelation(inBO);
			
			ArrayList<BusiObj> tablesBoList =retBo.getBusiObjList("SERVICE_TABLES");
			ArrayList<BusiObj> relationBoList =retBo.getBusiObjList("SERVICE_TABLE_RELATIONS");
			ArrayList<HashMap<String,String>>a1RelationColumn =new ArrayList<HashMap<String,String>>();
			ArrayList<String> aTablesName = new ArrayList<String> ();
			for(BusiObj tableBo:tablesBoList)
			{
				
				
				String service_table_id = tableBo.getString("SERVICE_TABLE_ID");
				String service_table_name	=	tableBo.getString("SERVICE_TABLE_NAME");
				String column_name	=	tableBo.getString("COLUMN_NAME");
				aTablesName.add(service_table_name);
				for(BusiObj relationBo:relationBoList)
				{
					String service_table_id_relation = relationBo.getString("SERVICE_TABLE_ID");
					if(service_table_id_relation.toUpperCase().equals(service_table_id.toUpperCase()))
					{
						HashMap<String,String>h1RelationColumns=new HashMap<String,String>();
						String relation_table_name	=	relationBo.getString("RELATION_TABLE_NAME");
						aTablesName.add(relation_table_name);
						String relation_column_name	=	relationBo.getString("RELATION_COLUMN_NAME");
						h1RelationColumns.put("TA", service_table_name);
						h1RelationColumns.put("TB", relation_table_name);
						h1RelationColumns.put(column_name, relation_column_name);
						a1RelationColumn.add(h1RelationColumns);
					}
				}
			}
			retBo=__CmPublic.getMultiTableDataForOneRS(aTablesName, conn, a1RelationColumn);
			
			//BusiObj retBo=__CmPublic.getMultiTableData(tablesName, conn, aRelationColumn);
			//BusiObj retBo=__CmPublic.getMultiTableDataForMultiRS(tablesName, conn, aRelationColumn);
			
			vLogger.info("返回数据是:\n\n\n\n" + BOHelper.boToXML(retBo));
/*			BusiObj		industryBO	=new BusiObj() ;
			industryBO.setString("INDUSTRY_TYPE", "0");
			industryBO.setString("INDUSTRY_NAME", "农业");
			t.vInBO.setValue("INDUSTRYLIST", industryBO);
			t.vInBO.setString("new_flag", "1");
			t.vInBO.setConnection(conn);
			t.vInBO.setName("setIndustryType");
			BusiObj retBo					=		ServiceBus.runService(t.vInBO);*/
			conn.rollback();
			//conn.commit();
			conn.close();
			//vLogger.info("返回数据是:\n\n\n\n" + BOHelper.boToXML(retBo));


		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		catch(BaseException e)
		{
			e.printStackTrace();
			vLogger.error(e.ErrMethod);
			vLogger.error(e.SysMsg);
		}
			vLogger.info("---------------Game Over-------------");
	}	


	private int  init()
	{
		int iRet = 0;
		vSD = new  SessionData();
		vSD.vRoleCode = new String[] {"1"};
		vSD.vStaffCode = "1";
		vSD.vStaffNo = "0001";
		vSD.vStaffName = "test";
		
	
		vDebugHome = System.getProperty("DEBUG_HOME");
		if (vDebugHome == null)
		{
			iRet = -1;
			vLogger.error("需要设置DEBUG_HOME环境变量来指定debug 文件所在的目录");
		}
		return iRet;
		
	}
	
}
